<template>
  <uni-popup ref="fillState" type="center" :is-mask-click="false">
    <view class="fillState">
      <view style="text-align: center;margin-bottom: 30rpx;">{{type==='1'?'转账到微信零钱':type==='2'?'是否发放到零钱':'不通过原因'}}
      </view>
      <view v-if="type==='1'">
        <view class="center">
          <view>微信号:{{weatch}}</view>
          <view class="copy" @click="copyText(weatch)">复制</view>
        </view>
        <mk-upload :imgList="imgList" @onDelete="onDelete" @onChoose="onChoose" :maxCount="1" />
      </view>
      <textarea class="textarea" v-model="des" placeholder="请填写不通过原因" v-if="type==='3'"></textarea>
      <Button @cancel="closeBankPad" @confirm="submit()" style="margin-top:20rpx"></Button>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">
  import Button from './buttom.vue';
  import { toPublish } from '@mqtt';
  import { copyText } from '@/utils/copyText';
  import mkUpload from "@c/mk-upload/mk-upload.vue"
  import { showLoading, errorToast, successToast } from '@/utils/prompt';
  import { ref } from 'vue';
  import { getDriverID } from '@/stores/driverID';
  const admin_id = getDriverID();
  const props = defineProps({
    weatch: { type: String },
    order_ID: { type: String },
    type: { type: String }
  });
  const emit = defineEmits(['submit']);
  const fillState = ref(null)
  const des = ref('')
  // 上传图片
  const imgList = ref([])
  // 上传图片
  function onChoose(e : any) {
    uni.uploadFile({
      url: 'https://admin.anlan.tech/api/common/upload2',
      filePath: e.tempFilePaths[0],
      name: 'file',
      formData: {
        'user': 'test'  // 上传附带参数
      },
      success: (uploadFileRes) => {
        // 根据接口具体返回格式   赋值具体对应url
        imgList.value.push(JSON.parse(uploadFileRes.data).data.fullurl)
      },
      fail: (err : any) => {
        console.log('err', err)
      }
    });
  }
  // 删除图片
  function onDelete() {
    imgList.value = []
  }
  function submit() {
    if (props.type === '1' && !imgList.value[0]) {
      errorToast('请上传您的转账凭证')
      return
    }
    closeBankPad()
    switch (props.type) {
      case '1':
        showLoading()
        const payload = {
          admin_id,
          order_id: props.order_ID,
          image: imgList.value[0],
          pay_type: 'admin'
        };
        toPublish(
          'control/doWithdraw',
          payload,
          (obj : any) => {
            const { code, msg } = obj;
            if (code === 1) {
              successToast(msg)
              emit('submit')
            } else {
              errorToast(msg)
            }
          }
        );
        break;
      case '2':
        showLoading()
        const payload2 = {
          admin_id,
          order_id: props.order_ID,
          image: imgList.value[0],
          pay_type: 'wechat'
        };
        toPublish(
          'control/doWithdraw',
          payload2,
          (obj : any) => {
            const { code, msg } = obj;
            if (code === 1) {
              successToast(msg)
              emit('submit')
            } else {
              errorToast(msg)
            }
          }
        );
        break;
      case '3':
        showLoading()
        const payload1 = {
          admin_id,
          order_id: props.order_ID,
          des: des.value
        };
        toPublish(
          'control/doWithdrawNo',
          payload1,
          (obj : any) => {
            const { code, msg } = obj;
            if (code === 1) {
              successToast(msg)
              emit('submit')
            } else {
              errorToast(msg)
            }
          }
        );
        break;
    }
  }
  // 开关提现信息
  function openBankPad() {
    imgList.value = []
    des.value = ''
    fillState.value.open();
  }
  function closeBankPad() {
    fillState.value.close();
  }
  defineExpose({ openBankPad });
</script>
<style scoped lang="less">
  .fillState {
    width: 650rpx;
    background-color: white;
    border-radius: 5px;
    padding: 30rpx 30rpx 40rpx 30rpx;
    box-sizing: border-box;

    .copy {
      padding: 2rpx 10rpx;
      box-sizing: border-box;
      border-radius: 5px;
      background-color: rgba(63, 144, 255, .3);
      color: #3f90ff;
      margin-left: 20rpx;
    }
  }
</style>
<style>
  .fillState>>>.textarea {
    width: 100%;
    height: 150rpx;
    background-color: #eee;
    border-radius: 5px;
    padding: 10rpx;
    box-sizing: border-box;
  }
</style>